import { userStore } from "@/store/userStore"
// import $http from "@/api/http"
import cloneDeep from "lodash/cloneDeep"


// 解决报错： The above dynamic import cannot be analyzed by Vite.   => https://blog.csdn.net/qq_53244180/article/details/128010168
const modules = import.meta.glob("../views/**/*.vue")

/**
 * 注册路由
 */
export const registerRoutes = async (router: object): Promise<boolean> => {
  return new Promise((resolve, reject) => {
    const routesMap = userStore().routesMap
    //已经获取过路由数据
    if (routesMap?.length) {
      const routesMap = userStore().routesMap
      const asyncRoutes = handlingRouters(cloneDeep(routesMap))

      // console.log("asyncRoutes ...", asyncRoutes)

      addRoutes(router, asyncRoutes)
      userStore().setIsRefer(false)
      resolve()
      return
    } else {
      // ajax 请求数据
      setTimeout(() => {
        console.log("ajax - routes")
        const routes = [
          // {
          //   path: "/layout",
          //   name: "project",
          //   component: "layout",
          //   icon: "MailOutlined",
          //   redirect: "/layout/foods",
          //   meta: {
          //     title: "项目管理",

          //   },
          //   children: [
          //     {
          //       path: "/layout/demand",
          //       name: "demand",
          //       component: "layout/demand",
          //       meta: {
          //         title: "设计需求",
          //       },
          //     },
          //     {
          //       path: "/layout/ProjectManage",
          //       name: "ProjectManage",
          //       component: "layout/ProjectManage",
          //       meta: {
          //         title: "项目管理",
          //       },
          //     },
          //     {
          //       path: "/layout/ProjectInfo",
          //       name: "ProjectInfo",
          //       component: "layout/ProjectInfo",
          //       meta: {
          //         title: "项目信息",
          //       },
          //       hidden: true,
          //       icon: "AppstoreOutlined",
          //     },
          //     {
          //       path: "/layout/ProjectPlane",
          //       name: "ProjectPlane",
          //       component: "layout/ProjectPlane",
          //       meta: {
          //         title: "项目计划",
          //       },
          //       hidden: true,
          //       icon: "AppstoreOutlined",
          //     },
          //   ]
          // },
          // {
          //   path: "/layout",
          //   name: "team",
          //   component: "layout",
          //   icon: "CalendarOutlined",
          //   redirect: "/layout/foods",
          //   meta: {
          //     title: "团队管理",

          //   },
          //   children: [

          //     {
          //       path: "/layout/teamMange",
          //       name: "teamMange",
          //       component: "layout/teamMange",
          //       meta: {
          //         title: "团队管理",
          //       },
          //     }
          //   ]
          // },
          // {
          //   path: "/layout",
          //   name: "resource",
          //   component: "layout",
          //   icon: "AppstoreOutlined",
          //   redirect: "/layout/foods",
          //   meta: {
          //     title: "申请资源",

          //   },
          //   children: [
          //     {
          //       path: "/layout/design",
          //       name: "design",
          //       component: "layout/design",
          //       meta: {
          //         title: "设计工具",
          //       },
          //     }
          //   ]
          // },

          //演示路由  - 基础组件
          {
            path: "/layout",
            name: "baseComponent",
            component: "layout",
            icon: "AppstoreOutlined",
            redirect: "/layout/baseComponent",
            meta: {
              title: "基础组件",
            },
            children: [
              {
                path: "/layout/baseTable",
                name: "baseTable",
                component: "layout/baseTable",
                meta: {
                  title: "表格组件",
                },
              },
              {
                path: "/layout/loginSlider",
                name: "loginSlider",
                component: "layout/loginSlider",
                meta: {
                  title: "登录滑动组件",
                },
              },
              {
                path: "/layout/upload",
                name: "upload",
                component: "layout/upload",
                meta: {
                  title: "上传组件",
                },
              },
              {
                path: "/layout/tree",
                name: "tree",
                component: "layout/tree",
                meta: {
                  title: "树形结构组件",
                },
              },
              {
                path: "/layout/modal",
                name: "modal",
                component: "layout/modal",
                meta: {
                  title: "模态框组件",
                },
              },

              {
                path: "/layout/drawer",
                name: "drawer",
                component: "layout/drawer",
                meta: {
                  title: "抽屉组件",
                },
              },
              {
                path: "/layout/404",
                name: "404",
                component: "layout/404",
                meta: {
                  title: "资源未找到组件",
                },
              },
              {
                path: "/layout/403",
                name: "403",
                component: "layout/403",
                meta: {
                  title: "没有权限组件",
                },
              },
              {
                path: "/layout/500",
                name: "500",
                component: "layout/500",
                meta: {
                  title: "服务器报错组件",
                },
              },
            ]
          },

          //演示路由  - 业务组件
          {
            path: "/layout",
            name: "businessComponent",
            component: "layout",
            icon: "CalendarOutlined",
            redirect: "/layout/businessComponent",
            meta: {
              title: "业务组件",

            },
            children: [
              {
                path: "/layout/workbench",
                name: "workbench",
                component: "layout/workbench",
                meta: {
                  title: "项目工作台组件",
                },
              },
              {
                path: "/layout/ganttChart",
                name: "ganttChart",
                component: "layout/ganttChart",
                meta: {
                  title: "任务甘特图组件",
                },
              },
              {
                path: "/layout/stateFlow",
                name: "stateFlow",
                component: "layout/stateFlow",
                meta: {
                  title: "状态流程组件",
                },
              },
              {
                path: "/layout/htmlEditor",
                name: "htmlEditor",
                component: "layout/htmlEditor",
                meta: {
                  title: "html编辑器",
                },
              },
              {
                path: "/layout/viewEditHtml",
                name: "viewEditHtml",
                component: "layout/viewEditHtml",
                meta: {
                  title: "视图编辑器组件管理端",
                },
              },
              {
                path: "/layout/viewEditHtmlHome",
                name: "viewEditHtmlHome",
                component: "layout/viewEditHtmlHome",
                meta: {
                  title: "视图编辑器门户",
                },
              },
              {
                path: "/layout/ratingBar",
                name: "ratingBar",
                component: "layout/RatingBar",
                meta: {
                  title: "评分组件",
                },
              },
              {
                path: "/layout/teamManagement",
                name: "teamManagement",
                component: "layout/teamManagement",
                meta: {
                  title: "团队管理组件",
                },
              },
              {
                path: "/layout/crateFrom",
                name: "crateFrom",
                component: "layout/crateFrom",
                meta: {
                  title: "创建项目和创建任务表单",
                },
              },
              {
                path: "/layout/resources",
                name: "resources",
                component: "layout/resources",
                meta: {
                  title: "资源选择公共表单",
                },
              },


            ]
          },

          //场景展示
          {
            path: "/layout",
            name: "scenecase",
            component: "layout",
            icon: "MailOutlined",
            redirect: "/layout/scenecase",
            meta: {
              title: "场景展示",

            },
            children: [
              {
                path: "/layout/watermark",
                name: "watermark",
                component: "layout/watermark",
                meta: {
                  title: "水印案例",
                },
              },
              {
                path: "/layout/theme",
                name: "theme",
                component: "layout/theme",
                meta: {
                  title: "动态主题",
                },
              },
              {
                path: "/layout/iptable",
                name: "iptable",
                component: "layout/iptable",
                meta: {
                  title: "IP列表",
                },
              }
            ]
          }
        ]
        userStore().setRoutesMap(cloneDeep(routes))
        const asyncRoutes = handlingRouters(cloneDeep(routes))
        addRoutes(router, asyncRoutes)
        resolve()
        //失败用 reject()
        console.log(reject)
      })
    }
  })
}

/**
 * 动态添加路由
 */
const handlingRouters = (routes: any): void => {
  return routes.map((r: any) => {
    if (r.path && r.component) {
      if (r.component === "Layout") {
        console.log(11)
        const compPath = r.component
        r.component = modules[/* @vite-ignore*/ "../views/layout.vue"]
        if (r.children?.length > 0) r.children = handlingRouters(r.children)
        return r
      } else {
        const compPath = r.component
        // import 里面不能用@ ，也不能用``字符拼接  ../list/books.vue
        // r.component = () => import('../views/' + compPath + '.vue');

        // 必须加 .vue扩展名，否则会报错： vue3启动报plugin:vite:import-analysis错
        // r.component = () => modules[`../views${compPath}.vue`]

        r.component = modules[/* @vite-ignore*/ `../views/${compPath}.vue`]

        // r.component = modules["/src/views" + compPath + ".vue"];
      }

      if (r.children?.length > 0) r.children = handlingRouters(r.children)

      return r
    }
  })
}

/**
 *
 * @param router {{object}} router实例对象
 * @param asyncRoutes {{array}} 处理好的路由
 */
const addRoutes = (router: object, asyncRoutes: any): void => {
  asyncRoutes.forEach(r => {
    router.addRoute(r)
  })

  //将此路由从静态路由表去除，放到动态权限路由获取相关函数中，在动态权限路由添加之后添加即可
  router.addRoute({
    path: "/:pathMatch(.*)*",
    redirect: "/404",
  })
}
